package com.zt.dev.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.dev.item.mapper.SystemItemMapper;
import com.zt.dev.item.pojo.SystemItem;
import com.zt.dev.item.service.SystemItemService;
import com.zt.dev.item.vo.SystemItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author 30378
 */
@Service
public class SystemItemServiceImpl implements SystemItemService {

    private final SystemItemMapper mapper;

    public SystemItemServiceImpl(SystemItemMapper mapper) {
        this.mapper = mapper;
    }

    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    @Override
    public int addItem(SystemItemVo systemItemVo) {
        SystemItem item = new SystemItem();
        BeanUtils.copyProperties(systemItemVo, item);
        return  mapper.insert(item);
    }

    @Override
    public void delItem(Integer[] ids) {
        mapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public SystemItem getItemById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public int editItem(SystemItemVo systemItemVo) {
        SystemItem item = new SystemItem();
        BeanUtils.copyProperties(systemItemVo, item);
        return mapper.updateById(item);
    }

    @Override
    public Page getAllItems(Page page) {
        QueryWrapper queryWrapper = new QueryWrapper();
        return mapper.selectMapsPage(page, queryWrapper);
    }

    @Override
    public boolean checkSystemZbName(String zbName) {
        Map map = new HashMap();
        map.put("zb_name", zbName);
        List list = mapper.selectByMap(map);
        if (list.size() == 0) {
            return true;
        }
        return false;
    }
}
